﻿using OGT.FileTransferClient;
using OGT.FileTransferClient.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WYGDocumentTransferServiceDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string _documentTransferServicePath;
        private string _configSavePath = @"C:\Users\Administrator\Desktop\configs";
        private string _configName = "文档传输测试";
        private DataTable filesTable = new DataTable();
        private ITransferQueueService _service;
        private void btnConn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtAddress.Text))
                _service = GetTransferQueueService(_configSavePath, _configName, txtAddress.Text.Trim(), 10, 0.1, null,true);
            if (_service != null)
                lblFlag.Appearance.ForeColor = Color.Green;
        }
        private ITransferQueueService GetTransferQueueService(string configSavePath, string configName,string transferServiceAddress,int retryTimes,double retryInterval,Dictionary<string,string> extroInfos,bool allowEncrypt)
        {
            TransferQueueConfig transferConfig = new TransferQueueConfig();
            transferConfig.Address = transferServiceAddress;
            transferConfig.AllowEncrypt = allowEncrypt;
            transferConfig.ExtraInfos = extroInfos;
            transferConfig.RetryInterval = retryInterval;
            transferConfig.RetryTimes = retryTimes;
            if (!Directory.Exists(configSavePath))
                Directory.CreateDirectory(configSavePath);
            transferConfig.SavePath = configSavePath;

            if (!TransferQueueManager.ServiceCache.ContainsKey(configName))
                TransferQueueManager.RegisterTransferConfig(configName, transferConfig);

            ITransferQueueService service = TransferQueueManager.GetTransferQueueService(configName);
            service.InitializeService();
            return service;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            _documentTransferServicePath = ConfigMgr.GetServer();
            txtAddress.Text = _documentTransferServicePath;
            //InitializeTreeList();
        }

        private void InitializeTreeList()
        {
            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ConfigMgr.Save(txtAddress.Text.Trim());
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            try
            {
                if (_service != null)
                    DownloadFileAsync();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DownloadFileAsync()
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
                _service.DownloadFileAsync(null, "d21ee49c-a4d0-4f4c-9941-0f4d9da92717", dlg.SelectedPath + "\\" + "曲9-斜161井完井地质总结报告.xls", null);
            _service.DownloadCompleted += _service_DownloadCompleted;
            _service.DownloadException += _service_DownloadException;
        }

        private void _service_DownloadException(object sender, TransferExceptionEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void _service_DownloadCompleted(object sender, TransferCompletedEventArgs e)
        {
            MessageBox.Show(e.SourceFile+"download succeed");
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
                UploadFile(dlg.FileName);
        }
        private void UploadFile(string fileName)
        {
            try
            {
                _service.UploadFileAsync(fileName, null, Path.GetFileName(fileName), null);
                _service.UploadCompleted += _service_UploadCompleted;
                _service.UploadException += _service_UploadException;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void _service_UploadException(object sender, TransferExceptionEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void _service_UploadCompleted(object sender, TransferCompletedEventArgs e)
        {
         
        }

        private void btnLookProgress_Click(object sender, EventArgs e)
        {
            if (_service != null)
                TransferQueueManager.GetTransferManageWindow(_configName).Show();
        }

        private void btnViewPdf_Click(object sender, EventArgs e)
        {
            if (_service == null)
                return;
            try
            {
                Stream stream=  _service.DownloadFile(null, "ced4b0ed-1e42-4605-88c5-dad6ed0edad8");
                //stream.Seek(0, SeekOrigin.Begin);
                //byte[] data = new byte[1024 * 1024 * 15];
                //stream.Read(data, 0, data.Length);
                //MemoryStream ms = new MemoryStream(data);
                pdfViewer1.LoadDocument((MemoryStream)stream);

                using (FileStream fs = new FileStream(@"C:\$1\b.pdf", FileMode.Create, FileAccess.Write))
                {
                    byte[] bytes = new byte[1024 * 1024 * 10];
                    int num = stream.Read(bytes, 0, bytes.Length);
                    while (num > 0)
                    {
                        fs.Write(bytes, 0, num);
                        num = stream.Read(bytes, 0, bytes.Length);
                    }
                }
                MessageBox.Show("ok");
                Stream stream2 = new FileStream(@"C:\$1\b.pdf", FileMode.Open, FileAccess.Read);
                pdfViewer1.LoadDocument(stream2);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private byte[] ConvertStreamToMemorystream(Stream stream)
        {
            byte[] bytes = new byte[1024*1024*10];
            int num = stream.Read(bytes, 0, bytes.Length);
            while(num>0)
            {

            }
        }
    }
}
